//
//  GuardRankViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/4/13.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "GuardRankViewController.h"
#import "GuardTopView.h"
#import "GuardHotCell.h"
#import "GuardNormalCell.h"
#import "GuardListModel.h"

@interface GuardRankViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *_guardListArray;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSString *guradNums;
@property (nonatomic, copy) NSString *popular;
@end

@implementation GuardRankViewController

- (void)guardListRequest {
    NSDictionary *dict;
    NSString *urlstring;
    if ([[[LSUserInfoManager sharedManager] currentUserInfo].u_type intValue] == 100) {
        dict = @{@"udid":self.s_uid,
                   @"token":TOKEN,
                   @"page":@"1"
                   };
        urlstring = [TEST_URL stringByAppendingString:KSGuardList];
    } else {
        dict = @{@"udid":User_ID,
                 @"token":TOKEN,
                 @"suid":self.s_uid,
                 @"page":@"1"
                 };
        urlstring = [TEST_URL stringByAppendingString:kFGuardList];
    }
    [NetworkTools postReqeustWithParams:dict UrlString:urlstring successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray *array = responseObject[@"data"][@"list"];
            self.guradNums = [NSString stringWithFormat:@"%@ 守护",responseObject[@"data"][@"num"]];
            self.popular = [NSString stringWithFormat:@"人气 %@",responseObject[@"data"][@"obtain"]];
            for (NSDictionary *guardDic in array) {
                GuardListModel *model = [[GuardListModel alloc] initWithDict:guardDic];
                [_guardListArray addObject:model];
            }
            [self.tableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _guardListArray = [NSMutableArray arrayWithCapacity:1];
    [self guardListRequest];
    [self setUpUI];
    self.navigationItem.title = @"守护排行榜";
}
#pragma mark -- UI
- (void)setUpUI {
    //tableView
    [self creatTableView];
    
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH,kSCREEN_HEIGHT - kNAV_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}

#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _guardListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GuardListModel *model = _guardListArray[indexPath.row];
    if (indexPath.row < 3) {
        GuardHotCell *cell = [GuardHotCell loadNibCellWithTabelView:tableView];
        cell.guardListModel = model;
        switch (indexPath.row) {
            case 0:
                cell.guardOrderImg.image = kSET_IMAGE(@"guard_first");
                break;
            case 1:
                cell.guardOrderImg.image = kSET_IMAGE(@"guard_second");
                break;
            case 2:
                cell.guardOrderImg.image = kSET_IMAGE(@"guard_third");
                break;
            default:
                break;
        }
        return cell;
    }
    GuardNormalCell *cell = [GuardNormalCell loadNibCellWithTabelView:tableView];
    cell.OrderLabel.text = [NSString stringWithFormat:@"No.%ld",(long)indexPath.row+1];
    cell.guardListModel = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -- 区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    GuardTopView *topView = [[NSBundle mainBundle] loadNibNamed:@"GuardTopView" owner:self options:nil].lastObject;
     topView.guardNums.text = self.guradNums;
     topView.popularity.text = self.popular;
    return topView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < 3) {
        return 180;
    }
    return 60;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
